<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\BaseController;
use app\lib\exception\BaseException;
use app\model\Toke;
use app\model\Warehouse;
use think\facade\Db;

//出仓
class Tokes extends BaseController
{
    public function index($limit=10)
    {
        $name = request()->param('name');
        $status = request()->param('status',1);
        $time = request()->param('time');
        $school = request()->param('school_id');
        $data=Toke::with('school')->where('status',$status)->where('school_id',$school)
            ->when($name, function ($query) use ($name) {
                $query->where('number', 'like', "%$name%");
            })->when($status, function ($query) use ($status) {
                $query->where('status', $status);
            })->when($time, function ($query) use ($time) {
                $query->whereBetween('create_time', $time);
            })->order('id','desc')->paginate($limit);

        return $this->success($data);
    }

    public function create()
    {
        $params=request()->param();
        Db::startTrans();
        try {
            if(empty($params['product'])){
                throw new \Exception('請至少添加一條產品!');
            }else{
                $product=json_decode($params['product'],true);
                foreach ($product as $item){
                    $warehouse = Warehouse::where('school_id', $params['school_id'])->where('product_id', $item['id'])->find();
                    if(!$warehouse){
                        throw new \Exception('本學院貨倉沒有' . $item['name'] . '該產品,請前往添加後再操作！');
                    }else{
                        if($warehouse->num>=$item['num']){
                            $number =$warehouse->num - $item['num']  ;
                            $warehouse::where('id', $warehouse->id)->update(['num' => $number, 'points_num' => $number]);
                        }else{
                            throw new \Exception('產品：' . $item['name'] . '庫存不足，請調整庫存后再進行操作！');
                        }
                    }
                }
                $params['number']=getNumber('出倉');
                Toke::create($params);
                Db::commit();
                return $this->success();
            }
        }catch (\Exception $e){
            Db::rollback();
            throw new BaseException(['msg'=>$e->getMessage()]);
        }

    }

    public function update($id)
    {
        $params=request()->param();
        Db::startTrans();

        try {
            if(empty($params['product'])){
                throw new \Exception('請至少添加一條產品!');
            }else{
                $product=json_decode($params['product'],true);
                foreach ($product as $item){
                    $warehouse = Warehouse::where('school_id', $params['school_id'])->where('product_id', $item['id'])->find();
                    if(!$warehouse){
                        throw new \Exception('本學院貨倉沒有' . $item['name'] . '該產品,請前往添加後再操作！');
                    }else{
                        if($warehouse->num>=$item['num']){
                            $number =$warehouse->num - $item['num']  ;
                            $warehouse::where('id', $warehouse->id)->update(['num' => $number, 'points_num' => $number]);
                        }else{
                            throw new \Exception('產品：' . $item['name'] . '庫存不足，請調整庫存后再進行操作！');
                        }
                    }
                }
                Toke::where('id',$id)->update($params);
                Db::commit();
                return $this->success();
            }
        }catch (\Exception $e){
            Db::rollback();
            throw new BaseException(['msg'=>$e->getMessage()]);
        }

    }

    public function delete($id)
    {
        Toke::where('id','in',$id)->update(['status'=>-1]);
        return $this->success();
    }
}
